<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 找出字符串中第一个匹配项的下标 -->
    <script>
      // var strStr = function (haystack, needle) {
      //   return haystack.indexOf(needle)
      // }

      var strStr = function (haystack, needle) {
        if (needle.length === 0) return 0
        for (let i = 0; i < haystack.length; i++) {
          if (haystack[i] === needle[0]) {
            if (haystack.slice(i, i + needle.length) === needle) {
              return i
            }
          }
        }
        return -1
      }
      console.log(strStr('sadbutsad', 'sad'))
    </script>
  </body>
</html>
